import 'dart:async';

import 'package:flutter/material.dart';

class CountDownSkip extends StatefulWidget {
  final Function function;

  CountDownSkip({
    Key key,
    @required this.function,
  }) : super(key: key);

  @override
  _CountDownSkipState createState() => _CountDownSkipState();
}

class _CountDownSkipState extends State<CountDownSkip> {
  Timer _timer;
  int _start = 3;

  @override
  void initState() {
    super.initState();

    _timer = Timer.periodic(
      Duration(seconds: 1),
      (timer) {
        setState(() {
          if (_start <= 1) {
            timer.cancel();
            widget.function();
          } else {
            _start--;
          }
        });
      },
    );
  }

  @override
  void dispose() {
    _timer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Container(
        decoration: BoxDecoration(
          color: Colors.black45,
          borderRadius: BorderRadius.all(Radius.circular(16)),
        ),
        padding: EdgeInsets.symmetric(vertical: 2, horizontal: 8),
        child: Text(
          '$_start | 跳过',
          style: TextStyle(color: Colors.white),
        ),
      ),
      onTap: widget.function,
    );
  }
}
